﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using QxFramework.Core;
public class BenchInteraction :Interaction
{
    BoxCollider2D box;
    [HideInInspector]
    bool isOpen = false;
    public bool IsOpen
    {
        get
        {
            return isOpen;
        }
        set
        {
            isOpen = false;
        }
    }
    public override void Invoke(CombinableItemField info)
    {
        base.Invoke(info);
        if (box == null)
        {
            box = this.transform.Find("TriggerBox").GetComponent<BoxCollider2D>();
        }
        // Collider2D[] ans = new Collider2D[50];
        /*  List<Collider2D> ans = new List<Collider2D>();
          var filter = new ContactFilter2D();

      //    filter.useLayerMask = true;
          filter.useTriggers = true;
        //  filter.SetLayerMask(this.gameObject.layer);
        // GameObject.findw
          Debug.Log(box.OverlapCollider(filter, ans));
          foreach(var x in ans)
          {
         //     if (x != null)
              {
                  Debug.Log(LayerMask.LayerToName(x.gameObject.layer));
              }
          }
          var list = new List<CombinableItem>();
          foreach (var x in ans)
          {
              if (x != null && x.gameObject.GetComponentInParent<CombinableItem>())
              {
                  list.Add(x.gameObject.GetComponentInParent<CombinableItem>());

              }
          }*/
        if(GameCombine.CombineManager.Get<GameCombine.ICombineUIManager>().IsBenchOpening==true){
            return;
            }
        var list = box.GetInColliderParent<CombinableItem>(LayerMask.NameToLayer("Player"));
        if (box.GetInColliderParent<CombinableItem>(LayerMask.NameToLayer("Enemy")).Count != 0)
        {
            GameObject clone = ResourceManager.Instance.Instantiate("Prefabs/Effects/DamageText");
            clone.transform.position = this.transform.position;
            clone.GetComponentInChildren<TextMesh>().text = "附近有敌人出没";
            clone.GetComponentInChildren<TextMesh>().color = Color.red;
            return;
        }
   //     list.RemoveAll(s => s.gameObject.layer == LayerMask.NameToLayer("Enemy"));
      
        MessageManager.Instance.Get<GameControl.GameAction>().DispatchMessage(GameControl.GameAction.OpenBenchUI, this, new BenchArgs
        {
            collectedItems = list,
            interactItemName = info.ItemName,
            benchName = this.gameObject.name,
            user = GameControl.GameManager.Instance.User,
            isFinalBench = this.transform.name.StartsWith("Final")
        }); ;

    }
}
public class MusicArgs : System.EventArgs
{
    public string name;
}
    public class BenchArgs : System.EventArgs
    {
        public List<CombinableItem> collectedItems;
        public string  interactItemName;
        public string user;
        public string benchName;
        public bool isFinalBench = false;
        
    }
